﻿using Quartz;
using Quartz.Impl;
using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FRMS.BUS;
using FRMS.Controllers;

namespace FRMS
{
    public class WorkAbnormalAppService : IJob
    {
        /*public async Task Execute(IJobExecutionContext context)
        {
            //增加定时检测设备状态功能

            ResponseMessage message = new ResponseMessage();

            DeviceDAL da = new DeviceDAL();
            message.data = da.QueryDeviceInfoToType();
            da.GetDeviceStateToType(message);
            List<DeviceInfo> list = da.QueryTKDevice();
            VoucherBUS bUS = new VoucherBUS();
            bUS.CheckStatus(list);
            return;

        }*/

        public async Task Execute(IJobExecutionContext context)
        {
            //增加定时检测设备状态功能
            DeviceDAL da = new DeviceDAL();
            DeviceInfo info = new DeviceInfo();
            List<DeviceInfo> list = da.QueryDeviceList(info);
            Dictionary<string, string> keys = DataDownLoad.device_dic;
            if (list != null)
            {
                foreach (var item in list)
                {
                    string time;
                    bool result = keys.TryGetValue(item.DeviceNO, out time);
                    if (result)
                    {
                        DateTime dt2 = Convert.ToDateTime(time);
                        DateTime dt1 = DateTime.Now;
                        TimeSpan ts = dt1.Subtract(dt2);
                        double seconds = ts.TotalSeconds;
                        if (seconds > 180)
                        {
                            if (item.DeviceState == "在线")
                            {
                                da.UpdateDeviceState(item.ID, "离线");
                            }
                        }
                        else
                        {
                            if (item.DeviceState == "离线")
                            {
                                da.UpdateDeviceState(item.ID, "在线");
                            }
                        }
                    }
                    else
                    {
                        if (item.DeviceState == "在线")
                        {
                            da.UpdateDeviceState(item.ID, "离线");
                        }
                    }
                }
            }
        }
        }
}
